import { ratioData } from '@/constant'
import type { Size } from '@/typing'

// /** Feed 的自带的模型，已选择的模型 */
export const selectedModelsProvider = 'selectedModelsProvider'
export const ratioVal = (size: Size) => {
  return size.w / size.h
}
/**
 * 比例字符串
 * @param num1 数字 1
 * @param num2 数字 2
 * @returns 返回冒号格式的字符串
 */
export const formatRatioToText = (size: Size) => {
  const [w, h] = calculateRatio(size.w, size.h)
  return `${w} : ${h}`
}

/**
 * 计算数字的比例
 * @param w 宽
 * @param h 高
 * @returns 比例
 */
export const calculateRatio = (w: number, h: number): number[] => {
  const gcd = (a: number, b: number): number => {
    return b === 0 ? a : gcd(b, a % b)
  }

  const divisor = gcd(w, h)
  const ratioW = w / divisor
  const ratioH = h / divisor

  return [ratioW, ratioH]
}
/** 查找比例索引 */
export const findRatioIndex = (size: Size) => {
  for (let i = 0; i < ratioData.length; i++) {
    const item = ratioData[i]
    if (item.w === size.w && item.h === size.h) {
      return i
    }
  }
  return 0
}


